package org.jtao.gbsubject.service;

import org.jtao.gbsubject.domain.CurrentState;
import org.jtao.gbsubject.domain.Indicator;
import org.jtao.gbsubject.domain.Subject;
import org.jtao.gbsubject.model.CurrentStateExcelModel;
import org.jtao.gbsubject.repository.CurrentStateRepository;
import org.jtao.gbsubject.repository.IndicatorRepository;
import org.jtao.gbsubject.repository.SubjectRepository;
import org.jtao.gbsubject.service.dto.CurrentStateDTO;
import org.jtao.gbsubject.service.mapper.CurrentStateMapper;
import org.jtao.gbsubject.service.mapper.IndicatorMapper;
import org.jtao.gbsubject.service.mapper.SubjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * Service Implementation for managing CurrentState.
 */
@Service
@Transactional
public class CurrentStateService {

    private final Logger log = LoggerFactory.getLogger(CurrentStateService.class);

    private final CurrentStateRepository currentStateRepository;

    private final CurrentStateMapper currentStateMapper;

    private final IndicatorRepository indicatorRepository;

    private final SubjectRepository subjectRepository;

    private final SubjectService subjectService;

    private final SubjectMapper subjectMapper;

    private final IndicatorService indicatorService;

    private final IndicatorMapper indicatorMapper;

    int sum=0;

    public CurrentStateService(CurrentStateRepository currentStateRepository, CurrentStateMapper currentStateMapper, IndicatorRepository indicatorRepository, SubjectRepository subjectRepository, SubjectService subjectService, SubjectMapper subjectMapper, IndicatorService indicatorService, IndicatorMapper indicatorMapper) {
        this.currentStateRepository = currentStateRepository;
        this.currentStateMapper = currentStateMapper;
        this.indicatorRepository = indicatorRepository;
        this.subjectRepository = subjectRepository;
        this.subjectService = subjectService;
        this.subjectMapper = subjectMapper;
        this.indicatorService = indicatorService;
        this.indicatorMapper = indicatorMapper;
    }

    /**
     * Save a currentState.
     *
     * @param currentStateDTO the entity to save
     * @return the persisted entity
     */
    public CurrentStateDTO save(CurrentStateDTO currentStateDTO) {
        log.debug("Request to save CurrentState : {}", currentStateDTO);
        CurrentState currentState = currentStateMapper.toEntity(currentStateDTO);
        currentState = currentStateRepository.save(currentState);
        return currentStateMapper.toDto(currentState);
    }

    /**
     * Get all the currentStates.
     *
     * @param pageable the pagination information
     * @return the list of entities
     */
    @Transactional(readOnly = true)
    public Page<CurrentStateDTO> findAll(Pageable pageable) {
        log.debug("Request to get all CurrentStates");
        return currentStateRepository.findAll(pageable)
            .map(currentStateMapper::toDto);
    }

    public Page<CurrentStateDTO> findAllBySubjectId(Long id) {
        log.debug("Request to get all CurrentStates by subjectId ");
        return currentStateRepository.findAllBySubjectId(id, null).map(currentStateMapper::toDto);
    }

    /**
     * Get one currentState by id.
     *
     * @param id the id of the entity
     * @return the entity
     */
    @Transactional(readOnly = true)
    public CurrentStateDTO findOne(Long id) {
        log.debug("Request to get CurrentState : {}", id);
        CurrentState currentState = currentStateRepository.findOne(id);
        return currentStateMapper.toDto(currentState);
    }

    /**
     * Delete the currentState by id.
     *
     * @param id the id of the entity
     */
    public void delete(Long id) {
        log.debug("Request to delete CurrentState : {}", id);
        currentStateRepository.delete(id);
    }

    public Integer importCurrentStates(MultipartFile file) {
        List<CurrentStateExcelModel> currentStateList = ImportService.importExcel(file, 0, 2, CurrentStateExcelModel.class);
        for (int i = 0; i < currentStateList.size(); i++) {
            CurrentStateExcelModel currentStateExcel = currentStateList.get(i);
            CurrentState currentStateFullTimeTeachers = fullTimeTeacherExcelToDomain(currentStateExcel);
            if (currentStateFullTimeTeachers != null) {
                currentStateRepository.save(currentStateFullTimeTeachers);
            }

            CurrentState currentStateMasterRadio = masterRatioExcelToDomain(currentStateExcel);
            if (currentStateMasterRadio != null) {
                currentStateRepository.save(currentStateMasterRadio);
            }

            CurrentState currentStateTotalCredits = totalCreditsExcelToDomain(currentStateExcel);
            if (currentStateTotalCredits != null) {
                currentStateRepository.save(currentStateTotalCredits);
            }

            CurrentState currentStateTeacherStudentRadio = teacherStudentRatioExcelToDomain(currentStateExcel);
            if (currentStateTeacherStudentRadio != null) {
                currentStateRepository.save(currentStateTeacherStudentRadio);
            }

        }
        System.out.println("导入数据一共【" + sum + "】行,");
        return sum;
    }

    public CurrentState fullTimeTeacherExcelToDomain(CurrentStateExcelModel currentStateExcel) {
        CurrentState currentStateNew = new CurrentState();
        String subjectName = currentStateExcel.getSubjectName();
        String indicatorName = currentStateExcel.getFullTimeTeachers().getIndicatorName();

        this.setSubjectAndIndicator(currentStateNew, subjectName, indicatorName);

        currentStateNew.setValue(currentStateExcel.getFullTimeTeachers().getCurrentValue());
        currentStateNew.setReferenceValue(currentStateExcel.getFullTimeTeachers().getReferenceValue());
        currentStateNew.setDeviation(currentStateExcel.getFullTimeTeachers().getDeviation());

        CurrentState currentStateDB = currentStateRepository.findOneBySubjectNameAndIndicatorName(subjectName, indicatorName);
        if (currentStateDB == null) {
            sum++;
            return currentStateNew;
        } else {
            currentStateNew.setId(currentStateDB.getId());
            return currentStateNew;
        }
    }

    public CurrentState masterRatioExcelToDomain(CurrentStateExcelModel currentStateExcel) {
        CurrentState currentStateNew = new CurrentState();
        String subjectName = currentStateExcel.getSubjectName();
        String indicatorName = currentStateExcel.getMasterRatio().getIndicatorName();

        this.setSubjectAndIndicator(currentStateNew, subjectName, indicatorName);

        currentStateNew.setValue(currentStateExcel.getMasterRatio().getCurrentValue());
        currentStateNew.setReferenceValue(currentStateExcel.getMasterRatio().getReferenceValue());
        currentStateNew.setDeviation(currentStateExcel.getMasterRatio().getDeviation());

        CurrentState currentStateDB = currentStateRepository.findOneBySubjectNameAndIndicatorName(subjectName, indicatorName);
        if (currentStateDB == null) {
            sum++;
            return currentStateNew;
        } else {
            currentStateNew.setId(currentStateDB.getId());
            return currentStateNew;
        }
    }

    public CurrentState totalCreditsExcelToDomain(CurrentStateExcelModel currentStateExcel) {
        CurrentState currentStateNew = new CurrentState();
        String subjectName = currentStateExcel.getSubjectName();
        String indicatorName = currentStateExcel.getTotalCredits().getIndicatorName();

        this.setSubjectAndIndicator(currentStateNew, subjectName, indicatorName);

        currentStateNew.setValue(currentStateExcel.getTotalCredits().getCurrentValue());
        currentStateNew.setReferenceValue(currentStateExcel.getTotalCredits().getReferenceValue());
        currentStateNew.setDeviation(currentStateExcel.getTotalCredits().getDeviation());

        CurrentState currentStateDB = currentStateRepository.findOneBySubjectNameAndIndicatorName(subjectName, indicatorName);
        if (currentStateDB == null) {
            sum++;
            return currentStateNew;
        } else {
            currentStateNew.setId(currentStateDB.getId());
            return currentStateNew;
        }
    }

    public CurrentState teacherStudentRatioExcelToDomain(CurrentStateExcelModel currentStateExcel) {
        CurrentState currentStateNew = new CurrentState();
        String subjectName = currentStateExcel.getSubjectName();
        String indicatorName = currentStateExcel.getTeacherStudentRatio().getIndicatorName();

        this.setSubjectAndIndicator(currentStateNew, subjectName, indicatorName);

        currentStateNew.setValue(currentStateExcel.getTeacherStudentRatio().getCurrentValue());
        currentStateNew.setReferenceValue(currentStateExcel.getTeacherStudentRatio().getReferenceValue());
        currentStateNew.setDeviation(currentStateExcel.getTeacherStudentRatio().getDeviation());

        CurrentState currentStateDB = currentStateRepository.findOneBySubjectNameAndIndicatorName(subjectName, indicatorName);
        if (currentStateDB == null) {
            sum++;
            return currentStateNew;
        } else {
            currentStateNew.setId(currentStateDB.getId());
            return currentStateNew;
        }
    }

    public void setSubjectAndIndicator(CurrentState currentState, String subjectName, String indicatorName) {
        Subject subjectDB = subjectRepository.findOneByName(subjectName);
        if (subjectDB != null) {
            currentState.setSubject(subjectDB);
        }else {
            Subject subject=new Subject();
            subject.setName(subjectName);
            subjectService.save(subjectMapper.toDto(subject));
            Subject subject1=subjectRepository.findOneByName(subjectName);
            currentState.setSubject(subject1);
        }

        Indicator indicatorDB = indicatorRepository.findOneByName(indicatorName);
        if (indicatorDB != null) {
            currentState.setIndicator(indicatorDB);
        } else {
            Indicator indicator = new Indicator();
            indicator.setName(indicatorName);
            indicatorService.save(indicatorMapper.toDto(indicator));
            Indicator indicator1 = indicatorRepository.findOneByName(indicatorName);
            currentState.setIndicator(indicator1);
        }
    }


}
